// app.js
const _api = require('./utils/api.js');
const apiurl = require('./utils/apiurl/public.js');
App({
  onLaunch() {
    // 展示本地存储能力
    this.globalData.systemInfo = tt.getSystemInfoSync();
    const Hasbindgetsource =  tt.canIUse('video-player.bindgetsource');
    this.setGlobalData('Hasbindgetsource',Hasbindgetsource)
    const logs = tt.getStorageSync('logs') || []
    logs.unshift(Date.now())
    tt.setStorageSync('logs', logs)
    tt.getSystemInfo({
        success: e => {
          this.globalData.StatusBar = e.statusBarHeight;
          let capsule = tt.getMenuButtonBoundingClientRect();
          if (capsule) {
               this.globalData.Custom = capsule;
              this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
          } else {
              this.globalData.CustomBar = e.statusBarHeight + 50;
          }
        }
      })
      let obj = tt.getLaunchOptionsSync();  
      console.log('tt.getDeviceInfoSync()',tt.getLaunchOptionsSync())
      let params={}             
      const query = obj.query; // 包含地址栏参数的对象
      if(query.clue_token && query.project_id){
        params ={
          clue_token:query.clue_token,
          project_id:query.project_id,
          promotion_id:query.promotion_id,
          advertiser_id:query.advertiser_id,
          req_id:query.req_id,
          data:JSON.stringify(query),
          mid1:query.mid1,
          mid2:query.mid2,
          mid3:query.mid3,
          mid4:query.mid4,
          mid5:query.mid5,
          type:'app'
      }
       
      }else{
        params={
          data:JSON.stringify(obj),
          type:'qqad'
        }
      }
     
      _api.post('index/user/dd_data_report',params).then(qdres=>{
      })
      tt.setStorageSync('source',params)
      tt.login({
        success:res=>{
          let code=res.code
          console.log('res',res);
          if(code){


            _api.post(apiurl.login,{js_code:code,code,code,anonymous_code:res.anonymousCode,type:'tt'}).then(loginres=>{

              console.log('loginres',loginres)
              if(loginres.data.code==0){
              tt.setStorageSync('token', loginres.data.data.token)
                 
                
                    _api.post('index/user/data_report',params).then(qdres=>{
                    })

                  //获取用户信息
                _api.get(apiurl.userinfo).then(res=>{
                      if(res.data.code==0){
                          wx.setStorageSync('getuser', res.data.data)
                      }
                  })
                }
              })














            return
            tt.getUserInfo({
              success:ures=>{
                console.log('code',ures)
                _api.post(apiurl.login,{js_code:code,encryptedData:ures.encryptedData,iv:ures.iv}).then(loginres=>{
                  tt.setStorageSync('token', loginres.data.data.token)
                     
                    
                        _api.post('index/user/data_report',params).then(qdres=>{
                        })

                      //获取用户信息
                    _api.get(apiurl.userinfo).then(res=>{
                          if(res.data.code==0){
                              wx.setStorageSync('getuser', res.data.data)
                          }
                      })
                  })
              },fail:errr=>{
                console.log('errr',errr)
              }
            })
        }
        }, fail:err=> {
          console.log(`login 调用失败`,err);
        },
      })


      if (tt.canIUse('getUpdateManager')) {
        const updateManager = tt.getUpdateManager()
        updateManager.onCheckForUpdate(function (res) {
         if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
           tt.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
             if (res.confirm) {
              updateManager.applyUpdate()
             }
            }
           })
          })
          updateManager.onUpdateFailed(function () {
           tt.showModal({
            title: '已经有新版本了哟~',
            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
           })
          })
         }
        })
       } else {
        tt.showModal({
         title: '提示',
         content: '当前版本过低，无法使用该功能，请升级到最新微信版本后重试。'
        })
       }
      


  }, setGlobalData(key, value) {
    this.globalData[key] = value;
  },
  globalData: {
    userInfo: null,
    ColorList: [{
        title: '嫣红',
        name: 'red',
        color: '#e54d42'
      },
      {
        title: '桔橙',
        name: 'orange',
        color: '#f37b1d'
      },
      {
        title: '明黄',
        name: 'yellow',
        color: '#fbbd08'
      },
      {
        title: '橄榄',
        name: 'olive',
        color: '#8dc63f'
      },
      {
        title: '森绿',
        name: 'green',
        color: '#39b54a'
      },
      {
        title: '天青',
        name: 'cyan',
        color: '#1cbbb4'
      },
      {
        title: '海蓝',
        name: 'blue',
        color: '#0081ff'
      },
      {
        title: '姹紫',
        name: 'purple',
        color: '#6739b6'
      },
      {
        title: '木槿',
        name: 'mauve',
        color: '#9c26b0'
      },
      {
        title: '桃粉',
        name: 'pink',
        color: '#e03997'
      },
      {
        title: '棕褐',
        name: 'brown',
        color: '#a5673f'
      },
      {
        title: '玄灰',
        name: 'grey',
        color: '#8799a3'
      },
      {
        title: '草灰',
        name: 'gray',
        color: '#aaaaaa'
      },
      {
        title: '墨黑',
        name: 'black',
        color: '#333333'
      },
      {
        title: '雅白',
        name: 'white',
        color: '#ffffff'
      },
    ]
  }
})
